package com.ihr360.expr.function;

import com.ihr360.expr.ExprException;
import com.ihr360.expr.context.IEvaluationContext;
import com.ihr360.expr.core.Expr;
import com.ihr360.expr.core.ExprFunction;
import com.ihr360.expr.core.IExprFunction;
import com.ihr360.expr.function.excel.AND;
import com.ihr360.expr.function.excel.DAY;
import com.ihr360.expr.function.excel.FIXED;
import com.ihr360.expr.function.excel.HLOOKUP;
import com.ihr360.expr.function.excel.IF;
import com.ihr360.expr.function.excel.MONTH;
import com.ihr360.expr.function.excel.OR;
import com.ihr360.expr.function.excel.ROUND;
import com.ihr360.expr.function.excel.ROUNDDOWN;
import com.ihr360.expr.function.excel.ROUNDUP;
import com.ihr360.expr.function.excel.SUM;
import com.ihr360.expr.function.excel.SUMIF;
import com.ihr360.expr.function.excel.VLOOKUP;
import com.ihr360.expr.function.excel.YEAR;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author davidwei
 */
public class ExcelFunctionProvider implements IFunctionProvider {
    private static Map<String, IExprFunction> functions = new HashMap();

    static {
        functions.put("IF",new IF());
        functions.put("SUM", new SUM());
        functions.put("SUMIF",new SUMIF());
        functions.put("AND",new AND());
        functions.put("FIXED",new FIXED());
        functions.put("VLOOKUP", new VLOOKUP());
        functions.put("HLOOKUP",new HLOOKUP());
        functions.put("ROUND",new ROUND());
        functions.put("ROUNDDOWN",new ROUNDDOWN());
        functions.put("ROUNDUP",new ROUNDUP());
        functions.put("OR",new OR());
        functions.put("YEAR",new YEAR());
        functions.put("MONTH",new MONTH());
        functions.put("DAY",new DAY());
    }

    public static Set<String> supportFunctions() {
        return functions.keySet();
    }


    @Override
    public boolean hasFunction(ExprFunction function) {
        return functions.containsKey(function.getName().toUpperCase());
    }

    @Override
    public Expr evaluate(IEvaluationContext context, ExprFunction function)
            throws ExprException {
        IExprFunction f = functions.get(function.getName().toUpperCase());
        if (f != null)
            return f.evaluate(context, function.getArgs());
        return null;
    }



}